
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>logback中文网|第八章：MDC</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="logback中文网包含了logback中文文档，最新资讯，应用案例等。logback 继承自 log4j，它建立在有十年工业经验的日志系统之上。它比其它所有的日志系统更快并且更小，包含了许多独特并且有用的特性。"><meta name="keywords" content="logback中文文档, logback api文档, logback log4j,springboot logback,logback日志类型">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="09第九章日志隔离.html" />
    
    
    <link rel="prev" href="07第七章Filters.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01第一章logback介绍.html">
            
                <a href="01第一章logback介绍.html">
            
                    
                    第一章：logback 介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="02第二章架构.html">
            
                <a href="02第二章架构.html">
            
                    
                    第二章：架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="03第三章logback的配置.html">
            
                <a href="03第三章logback的配置.html">
            
                    
                    第三章：logback 的配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="04第四章Appenders.html">
            
                <a href="04第四章Appenders.html">
            
                    
                    第四章：Appenders
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="05第五章Encoder.html">
            
                <a href="05第五章Encoder.html">
            
                    
                    第五章：Encoder
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="06第六章Layouts.html">
            
                <a href="06第六章Layouts.html">
            
                    
                    第六章：Layouts
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="07第七章Filters.html">
            
                <a href="07第七章Filters.html">
            
                    
                    第七章：Filters
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.9" data-path="08第八章MDC.html">
            
                <a href="08第八章MDC.html">
            
                    
                    第八章：MDC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="09第九章日志隔离.html">
            
                <a href="09第九章日志隔离.html">
            
                    
                    第九章：日志隔离
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="10第十章JMX配置器.html">
            
                <a href="10第十章JMX配置器.html">
            
                    
                    第十章：JMX 配置器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="11第十一章Joran.html">
            
                <a href="11第十一章Joran.html">
            
                    
                    第十一章：Joran
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="12第十二章Groovy配置.html">
            
                <a href="12第十二章Groovy配置.html">
            
                    
                    第十二章：Groovy 配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="13第十三章从log4j迁移.html">
            
                <a href="13第十三章从log4j迁移.html">
            
                    
                    第十三章：从 log4j 迁移
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="14第十四章Receivers.html">
            
                <a href="14第十四章Receivers.html">
            
                    
                    第十四章：Receivers
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="15第十五章使用SSL.html">
            
                <a href="15第十五章使用SSL.html">
            
                    
                    第十五章：使用 SSL
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >第八章：MDC</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>logback &#x8BBE;&#x8BA1;&#x7684;&#x76EE;&#x6807;&#x4E4B;&#x4E00;&#x662F;&#x5BA1;&#x8BA1;&#x4E0E;&#x8C03;&#x8BD5;&#x590D;&#x6742;&#x7684;&#x5206;&#x5E03;&#x5F0F;&#x5E94;&#x7528;&#x3002;&#x5927;&#x90E8;&#x5206;&#x7684;&#x5206;&#x5E03;&#x5F0F;&#x7CFB;&#x7EDF;&#x9700;&#x8981;&#x540C;&#x65F6;&#x5904;&#x7406;&#x591A;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x5728;&#x4E00;&#x4E2A;&#x7CFB;&#x7EDF;&#x5178;&#x578B;&#x7684;&#x591A;&#x7EBF;&#x7A0B;&#x5B9E;&#x73B0;&#x4E2D;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x7EBF;&#x7A0B;&#x5904;&#x7406;&#x4E0D;&#x540C;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x4E00;&#x79CD;&#x53EF;&#x80FD;&#x4F46;&#x662F;&#x4E0D;&#x5EFA;&#x8BAE;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x5728;&#x6BCF;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x5B9E;&#x4F8B;&#x5316;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x4E14;&#x72EC;&#x7ACB;&#x7684; logger&#xFF0C;&#x6765;&#x533A;&#x5206;&#x4E00;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x4E0E;&#x53E6;&#x4E00;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x65E5;&#x5FD7;&#x8F93;&#x51FA;&#x3002;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x4F1A;&#x5BFC;&#x81F4; logger &#x6025;&#x5267;&#x589E;&#x52A0;&#x5E76;&#x4E14;&#x4F1A;&#x589E;&#x52A0;&#x7EF4;&#x62A4;&#x6210;&#x672C;&#x3002;</p>
<p>&#x4E00;&#x79CD;&#x8F7B;&#x91CF;&#x7EA7;&#x7684;&#x6280;&#x672F;&#x662F;&#x7ED9;&#x6BCF;&#x4E2A;&#x4E3A;&#x5BA2;&#x6237;&#x7AEF;&#x670D;&#x52A1;&#x7684; logger &#x6253;&#x4E00;&#x4E2A;&#x6807;&#x8BB0;&#x3002;Neil Harrison  &#x5728; <em>Patterns for Logging Diagnostic Messages</em> in Pattern Languages of Program Design 3, edited by R. Martin, D. Riehle, and F. Buschmann (Addison-Wesley, 1997) &#x8FD9;&#x672C;&#x4E66;&#x4E2D;&#x63CF;&#x8FF0;&#x4E86;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x3002;logback &#x5728; SLF4J API &#x5229;&#x7528;&#x4E86;&#x8FD9;&#x79CD;&#x6280;&#x672F;&#x7684;&#x53D8;&#x4F53;&#xFF1A;&#x8BCA;&#x65AD;&#x4E0A;&#x4E0B;&#x6587;&#x6620;&#x5C04; (MDC)&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x7ED9;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x6253;&#x4E0A;&#x552F;&#x4E00;&#x7684;&#x6807;&#x8BB0;&#xFF0C;&#x7528;&#x6237;&#x9700;&#x8981;&#x5C06;&#x4E0A;&#x4E0B;&#x6587;&#x4FE1;&#x606F;&#x653E;&#x5230; <code>MDC</code> (Mapped Diagnostic Context &#x7684;&#x7F29;&#x5199;) &#x4E2D;&#x3002;&#x4E0B;&#x9762;&#x5217;&#x51FA;&#x4E86; MDC &#x7C7B;&#x4E2D;&#x4E3B;&#x8981;&#x7684;&#x90E8;&#x5206;&#x3002;&#x5B8C;&#x6210;&#x7684;&#x65B9;&#x6CD5;&#x5217;&#x8868;&#x8BF7;&#x67E5;&#x770B; <a href="http://www.slf4j.org/api/org/slf4j/MDC.html" target="_blank">MDC javadocs</a>&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> org.slf4j;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MDC</span> </span>{
  <span class="hljs-comment">// &#x5C06;&#x4E0A;&#x4E0B;&#x6587;&#x7684;&#x503C;&#x4F5C;&#x4E3A; MDC &#x7684; key &#x653E;&#x5230;&#x7EBF;&#x7A0B;&#x4E0A;&#x4E0B;&#x7684; map &#x4E2D;</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">put</span><span class="hljs-params">(String key, String val)</span></span>;

  <span class="hljs-comment">// &#x901A;&#x8FC7; key &#x83B7;&#x53D6;&#x4E0A;&#x4E0B;&#x6587;&#x6807;&#x8BC6;</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title">get</span><span class="hljs-params">(String key)</span></span>;

  <span class="hljs-comment">// &#x901A;&#x8FC7; key &#x79FB;&#x9664;&#x4E0A;&#x4E0B;&#x6587;&#x6807;&#x8BC6;</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">remove</span><span class="hljs-params">(String key)</span></span>;

  <span class="hljs-comment">// &#x6E05;&#x9664; MDC &#x4E2D;&#x6240;&#x6709;&#x7684; entry</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">clear</span><span class="hljs-params">()</span></span>;
}
</code></pre>
<p><code>MDC</code> &#x7C7B;&#x4E2D;&#x53EA;&#x5305;&#x542B;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x3002;&#x5B83;&#x8BA9;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x53EF;&#x4EE5;&#x5728; <em>&#x8BCA;&#x65AD;&#x4E0A;&#x4E0B;&#x6587;</em> &#x4E2D;&#x653E;&#x7F6E;&#x4FE1;&#x606F;&#xFF0C;&#x800C;&#x540E;&#x901A;&#x8FC7;&#x7279;&#x5B9A;&#x7684; logback &#x7EC4;&#x4EF6;&#x53BB;&#x83B7;&#x53D6;&#x3002;<code>MDC</code> &#x5728; <em>&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x7684;&#x57FA;&#x7840;&#x4E0A;</em> &#x7BA1;&#x7406;&#x4E0A;&#x4E0B;&#x6587;&#x4FE1;&#x606F;&#x3002;&#x901A;&#x5E38;&#xFF0C;&#x5F53;&#x4E3A;&#x4E00;&#x4E2A;&#x65B0;&#x5BA2;&#x6237;&#x7AEF;&#x542F;&#x52A8;&#x670D;&#x52A1;&#x65F6;&#xFF0C;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x4F1A;&#x5C06;&#x7279;&#x5B9A;&#x7684;&#x4E0A;&#x6587;&#x4FE1;&#x606F;&#x63D2;&#x5165;&#x5230; MDC &#x4E2D;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF; id&#xFF0C;&#x5BA2;&#x6237;&#x7AEF; IP &#x5730;&#x5740;&#xFF0C;&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x7B49;&#x3002;&#x5982;&#x679C; logback &#x7EC4;&#x4EF6;&#x914D;&#x7F6E;&#x5F97;&#x5F53;&#x7684;&#x8BDD;&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x5728;&#x6BCF;&#x4E2A;&#x65E5;&#x5FD7;&#x6761;&#x76EE;&#x4E2D;&#x5305;&#x542B;&#x8FD9;&#x4E9B;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;logback-classic &#x5B9E;&#x73B0;&#x7684; MDC&#xFF0C;&#x5047;&#x8BBE;&#x503C;&#x4EE5;&#x9002;&#x5F53;&#x7684;&#x9891;&#x7387;&#x653E;&#x7F6E;&#x3002;&#x8FD8;&#x9700;&#x6CE8;&#x610F;&#x7684;&#x4E00;&#x70B9;&#x662F;&#xFF0C;&#x5B50;&#x7EBF;&#x7A0B;&#x4E0D;&#x4F1A;&#x81EA;&#x52A8;&#x7EE7;&#x627F;&#x7236;&#x7EBF;&#x7A0B;&#x7684; MDC&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684; <a href="https://logback.qos.ch/xref/chapters/mdc/SimpleMDC.html" target="_blank">SimpleMDC</a> &#x8BF4;&#x660E;&#x4E86;&#x8FD9;&#x70B9;&#x3002;</p>
<blockquote>
<p>  Example: <em><a href="https://logback.qos.ch/xref/chapters/mdc/SimpleMDC.html" target="_blank">SimpleMDC.java</a></em></p>
</blockquote>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> chapters.mdc;

<span class="hljs-keyword">import</span> org.slf4j.Logger;
<span class="hljs-keyword">import</span> org.slf4j.LoggerFactory;
<span class="hljs-keyword">import</span> org.slf4j.MDC;

<span class="hljs-keyword">import</span> ch.qos.logback.classic.PatternLayout;
<span class="hljs-keyword">import</span> ch.qos.logback.core.ConsoleAppender;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SimpleMDC</span> </span>{
  <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> Exception </span>{

    <span class="hljs-comment">// &#x4F60;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x5728;&#x4EFB;&#x4F55;&#x65F6;&#x5019;&#x5C06;&#x503C;&#x653E;&#x5165; MDC &#x4E2D;    </span>
    MDC.put(<span class="hljs-string">&quot;first&quot;</span>, <span class="hljs-string">&quot;Dorothy&quot;</span>);

    [ SNIP ]

    Logger logger = LoggerFactory.getLogger(SimpleMDC.class);

    MDC.put(<span class="hljs-string">&quot;last&quot;</span>, <span class="hljs-string">&quot;Parker&quot;</span>);

    logger.info(<span class="hljs-string">&quot;Check enclosed.&quot;</span>);
    logger.debug(<span class="hljs-string">&quot;The most beautiful two words in English.&quot;</span>);

    MDC.put(<span class="hljs-string">&quot;first&quot;</span>, <span class="hljs-string">&quot;Richard&quot;</span>);
    MDC.put(<span class="hljs-string">&quot;last&quot;</span>, <span class="hljs-string">&quot;Nixon&quot;</span>);
    logger.info(<span class="hljs-string">&quot;I am not a crook.&quot;</span>);
    logger.info(<span class="hljs-string">&quot;Attributed to the former US president. 17 Nov 1973.&quot;</span>);
  }

  [ SNIP ]

}
</code></pre>
<p>main &#x65B9;&#x6CD5;&#x5728;&#x542F;&#x52A8;&#x7684;&#x65F6;&#x5019;&#x5728; <code>MDC</code> &#x4E2D;&#x5C06; <em>Dorothy</em> &#x5173;&#x8054;&#x5230; <em>first</em> &#x4E0A;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5728; <code>MDC</code> &#x653E;&#x7F6E;&#x5C3D;&#x53EF;&#x80FD;&#x591A;&#x7684;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x53CD;&#x6B63;&#x4F60;&#x5F00;&#x5FC3;&#x5C31;&#x597D;&#x3002;&#x591A;&#x6B21;&#x63D2;&#x5165;&#x540C;&#x4E00;&#x4E2A; key&#xFF0C;&#x65B0;&#x503C;&#x4F1A;&#x8986;&#x76D6;&#x65E7;&#x503C;&#x3002;&#x7136;&#x540E;&#x4EE3;&#x7801;&#x7EE7;&#x7EED;&#x914D;&#x7F6E; logback&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x7B80;&#x6D01;&#xFF0C;&#x6211;&#x4EEC;&#x9690;&#x85CF;&#x4E86;&#x914D;&#x7F6E;&#x6587;&#x4EF6; <a href="http://github.com/qos-ch/logback/blob/master/logback-examples/src/main/java/chapters/mdc/simpleMDC.xml" target="_blank">simpleMDC.xml</a> &#x4E2D;&#x7684;&#x5176;&#x5B83;&#x914D;&#x7F6E;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x4E00;&#x4E9B;&#x76F8;&#x5173;&#x7684;&#x914D;&#x7F6E;&#x3002;</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">appender</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;CONSOLE&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;ch.qos.logback.core.ConsoleAppender&quot;</span>&gt;</span> 
  <span class="hljs-tag">&lt;<span class="hljs-name">layout</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Pattern</span>&gt;</span>%X{first} %X{last} - %m%n<span class="hljs-tag">&lt;/<span class="hljs-name">Pattern</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">layout</span>&gt;</span> 
<span class="hljs-tag">&lt;/<span class="hljs-name">appender</span>&gt;</span>
</code></pre>
<p>&#x6CE8;&#x610F; <em>X%</em> &#x8F6C;&#x6362;&#x7B26;&#x5728; <code>PatternLayout</code> &#x4E2D;&#x7684;&#x7528;&#x6CD5;&#x3002;<em>X%</em> &#x8F6C;&#x6362;&#x7B26;&#x4F7F;&#x7528;&#x4E86;&#x4E24;&#x6B21;&#xFF0C;&#x4E00;&#x6B21;&#x7528;&#x4E8E; key &#x4E3A; <em>first</em>&#xFF0C;&#x4E00;&#x6B21;&#x7528;&#x4E8E; key &#x4E3A; <em>last</em>&#x3002;&#x5728;&#x83B7;&#x5F97;&#x4E86; <code>SimpleMDC.class</code> &#x4E2D;&#x7684; logger &#x4E4B;&#x540E;&#xFF0C;&#x901A;&#x8FC7;&#x4EE3;&#x7801;&#x5C06; <em>Parker</em> &#x5173;&#x8054;&#x5230; <em>last</em> &#x4E0A;&#x3002;&#x7136;&#x540E;&#x901A;&#x8FC7; logger &#x6253;&#x5370;&#x4E86;&#x4E24;&#x6761;&#x4E0D;&#x540C;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x6700;&#x540E;&#xFF0C;&#x901A;&#x8FC7;&#x4EE3;&#x7801;&#x91CD;&#x65B0;&#x8BBE;&#x7F6E;&#x4E86; <code>MDC</code> &#x4E3A;&#x4E0D;&#x540C;&#x7684;&#x503C;&#xFF0C;&#x5E76;&#x6253;&#x5370;&#x4E86;&#x51E0;&#x6761;&#x65E5;&#x5FD7;&#x8BF7;&#x6C42;&#x3002;&#x8FD0;&#x884C; SimpleMDC &#x5C06;&#x4F1A;&#x8F93;&#x51FA;&#xFF1A;</p>
<pre><code class="lang-java">Dorothy Parker - Check enclosed.
Dorothy Parker - The most beautiful two words in English.
Richard Nixon - I am not a crook.
Richard Nixon - Attributed to the former US president. <span class="hljs-number">17</span> Nov <span class="hljs-number">1973</span>.
</code></pre>
<p><code>SimpleMDC</code> &#x5C55;&#x793A;&#x4E86;&#x6B63;&#x786E;&#x914D;&#x7F6E; logback layout &#x5C31;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x8F93;&#x51FA; <code>MDC</code> &#x7684;&#x4FE1;&#x606F;&#x3002;&#x800C;&#x4E14;&#xFF0C;<code>MDC</code> &#x4E2D;&#x4FE1;&#x606F;&#x53EF;&#x4EE5;&#x88AB;&#x591A;&#x4E2A; logger &#x4F7F;&#x7528;&#x3002;</p>
<h3 id="&#x9AD8;&#x7EA7;&#x7528;&#x6CD5;">&#x9AD8;&#x7EA7;&#x7528;&#x6CD5;</h3>
<p>MDC &#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x670D;&#x52A1;&#x5668;&#x67B6;&#x6784;&#x4E2D;&#x6700;&#x4E3A;&#x91CD;&#x8981;&#x3002;&#x901A;&#x5E38;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x7684;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E3A;&#x591A;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x63D0;&#x4F9B;&#x670D;&#x52A1;&#x3002;&#x5C3D;&#x7BA1; MDC &#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x9759;&#x6001;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x662F;&#x4EE5;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E3A;&#x57FA;&#x7840;&#x6765;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x7684;&#xFF0C;&#x5141;&#x8BB8;&#x6BCF;&#x4E2A;&#x670D;&#x52A1;&#x7EBF;&#x7A0B;&#x90FD;&#x6253;&#x4E0A;&#x4E00;&#x4E2A; <code>MDC</code> &#x6807;&#x8BB0;&#x3002;<code>MDC</code> &#x4E2D;&#x7684; <code>put()</code> &#x4E0E; <code>get()</code> &#x64CD;&#x4F5C;&#x4EC5;&#x4EC5;&#x53EA;&#x5F71;&#x54CD;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x4E2D;&#x7684; <code>MDC</code>&#x3002;&#x5176;&#x5B83;&#x7EBF;&#x7A0B;&#x4E2D; <code>MDC</code> &#x4E0D;&#x4F1A;&#x53D7;&#x5230;&#x5F71;&#x54CD;&#x3002;&#x7ED9;&#x5B9A;&#x7684; <code>MDC</code> &#x4FE1;&#x606F;&#x5728;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x7684;&#x57FA;&#x7840;&#x4E0A;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x3002;&#x6BCF;&#x4E2A;&#x7EBF;&#x7A0B;&#x90FD;&#x6709;&#x4E00;&#x4EFD; <code>MDC</code> &#x7684;&#x62F7;&#x8D1D;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x5728;&#x5BF9; <code>MDC</code> &#x8FDB;&#x884C;&#x7F16;&#x7A0B;&#x65F6;&#xFF0C;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x6CA1;&#x6709;&#x5FC5;&#x8981;&#x53BB;&#x8003;&#x8651;&#x7EBF;&#x7A0B;&#x5B89;&#x5168;&#x6216;&#x8005;&#x540C;&#x6B65;&#x95EE;&#x9898;&#x3002;&#x5B83;&#x81EA;&#x5DF1;&#x4F1A;&#x5B89;&#x5168;&#x7684;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x95EE;&#x9898;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x6709;&#x4E00;&#x4E2A;&#x7A0D;&#x5FAE;&#x9AD8;&#x7EA7;&#x4E00;&#x70B9;&#x7684;&#x4F8B;&#x5B50;&#x3002;&#x5B83;&#x663E;&#x793A;&#x4E86;&#x5982;&#x4F55;&#x5728;&#x5BA2;&#x6237;&#x7AEF;-&#x670D;&#x52A1;&#x7AEF;&#x4E0A;&#x8BBE;&#x7F6E; <code>MDC</code>&#x3002;&#x4F8B; 7.2 &#x5C55;&#x793A;&#x7684;&#x662F;&#x670D;&#x52A1;&#x7AEF;&#x5B9E;&#x73B0; <code>NumberCruncher</code> &#x63A5;&#x53E3;&#x3002;<code>NumberCruncher</code> &#x63A5;&#x53E3;&#x4EC5;&#x4EC5;&#x53EA;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5; <code>factor()</code>&#x3002;&#x5BA2;&#x6237;&#x7AEF;&#x4F7F;&#x7528; RMI &#x6280;&#x672F;&#xFF0C;&#x8C03;&#x7528;&#x670D;&#x52A1;&#x5668;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E0A;&#x7684; <code>factor()</code> &#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x7684;&#x4E0D;&#x540C;&#x56E0;&#x5B50;&#x3002;</p>
<blockquote>
<p>Example 7.2 <em>NumberCruncher.java</em></p>
</blockquote>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> chapters.mdc;

<span class="hljs-keyword">import</span> java.rmi.Remote;
<span class="hljs-keyword">import</span> java.rmi.RemoteException;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">NumberCruncher</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Remote</span> </span>{
  <span class="hljs-keyword">int</span>[] factor(<span class="hljs-keyword">int</span> number) <span class="hljs-keyword">throws</span> RemoteException;
}
</code></pre>
<p>&#x4F8B; 7.3 &#x5C55;&#x793A;&#x4E86; <code>NumberCruncherServer</code> &#x5B9E;&#x73B0;&#x4E86; <code>NumberCruncher</code> &#x63A5;&#x53E3;&#x3002;&#x5B83;&#x7684;&#x4E3B;&#x8981;&#x65B9;&#x6CD5;&#x662F;&#x5728;&#x672C;&#x5730;&#x4E3B;&#x673A;&#x4E0A;&#x5BFC;&#x51FA;&#x4E00;&#x4E2A; RMI &#x6CE8;&#x518C;&#x8868;&#xFF0C;&#x5E76;&#x5728;&#x4E00;&#x4E2A;&#x4F17;&#x6240;&#x5468;&#x77E5;&#x7684;&#x7AEF;&#x53E3;&#x53F7;&#x4E0A;&#x63A5;&#x53D7;&#x8BF7;&#x6C42; (&#x53CD;&#x6B63;&#x6211;&#x662F;&#x4E0D;&#x77E5;&#x9053;&#x1F613;)&#x3002;</p>
<blockquote>
<p><em>Example 7.3</em> <em>NumberCruncherServer.java</em></p>
</blockquote>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> chapters.mdc;

<span class="hljs-keyword">import</span> java.rmi.RemoteException;
<span class="hljs-keyword">import</span> java.rmi.registry.LocateRegistry;
<span class="hljs-keyword">import</span> java.rmi.registry.Registry;
<span class="hljs-keyword">import</span> java.rmi.server.UnicastRemoteObject;
<span class="hljs-keyword">import</span> java.util.Vector;

<span class="hljs-keyword">import</span> org.slf4j.Logger;
<span class="hljs-keyword">import</span> org.slf4j.LoggerFactory;
<span class="hljs-keyword">import</span> org.slf4j.MDC;

<span class="hljs-keyword">import</span> ch.qos.logback.classic.LoggerContext;
<span class="hljs-keyword">import</span> ch.qos.logback.classic.joran.JoranConfigurator;
<span class="hljs-keyword">import</span> ch.qos.logback.core.joran.spi.JoranException;


<span class="hljs-comment">/**
 * A simple NumberCruncher implementation that logs its progress when
 * factoring numbers. The purpose of the whole exercise is to show the
 * use of mapped diagnostic contexts in order to distinguish the log
 * output from different client requests.
 * */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NumberCruncherServer</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">UnicastRemoteObject</span>
  <span class="hljs-keyword">implements</span> <span class="hljs-title">NumberCruncher</span> </span>{

  <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = <span class="hljs-number">1L</span>;

  <span class="hljs-keyword">static</span> Logger logger = LoggerFactory.getLogger(NumberCruncherServer.class);

  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">NumberCruncherServer</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> RemoteException </span>{
  }

  <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span>[] factor(<span class="hljs-keyword">int</span> number) <span class="hljs-keyword">throws</span> RemoteException {
    <span class="hljs-comment">// The client&apos;s host is an important source of information.</span>
    <span class="hljs-keyword">try</span> {
      MDC.put(<span class="hljs-string">&quot;client&quot;</span>, NumberCruncherServer.getClientHost());
    } <span class="hljs-keyword">catch</span> (java.rmi.server.ServerNotActiveException e) {
      logger.warn(<span class="hljs-string">&quot;Caught unexpected ServerNotActiveException.&quot;</span>, e);
    }

    <span class="hljs-comment">// The information contained within the request is another source</span>
    <span class="hljs-comment">// of distinctive information. It might reveal the users name,</span>
    <span class="hljs-comment">// date of request, request ID etc. In servlet type environments,</span>
    <span class="hljs-comment">// useful information is contained in the HttpRequest or in the  </span>
    <span class="hljs-comment">// HttpSession.</span>
    MDC.put(<span class="hljs-string">&quot;number&quot;</span>, String.valueOf(number));

    logger.info(<span class="hljs-string">&quot;Beginning to factor.&quot;</span>);

    <span class="hljs-keyword">if</span> (number &lt;= <span class="hljs-number">0</span>) {
      <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(number +
        <span class="hljs-string">&quot; is not a positive integer.&quot;</span>);
    } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (number == <span class="hljs-number">1</span>) {
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[] { <span class="hljs-number">1</span> };
    }

    Vector&lt;Integer&gt; factors = <span class="hljs-keyword">new</span> Vector&lt;Integer&gt;();
    <span class="hljs-keyword">int</span> n = number;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>; (i &lt;= n) &amp;&amp; ((i * i) &lt;= number); i++) {
      <span class="hljs-comment">// It is bad practice to place log requests within tight loops.</span>
      <span class="hljs-comment">// It is done here to show interleaved log output from</span>
      <span class="hljs-comment">// different requests. </span>
      logger.debug(<span class="hljs-string">&quot;Trying &quot;</span> + i + <span class="hljs-string">&quot; as a factor.&quot;</span>);

      <span class="hljs-keyword">if</span> ((n % i) == <span class="hljs-number">0</span>) {
        logger.info(<span class="hljs-string">&quot;Found factor &quot;</span> + i);
        factors.addElement(<span class="hljs-keyword">new</span> Integer(i));

        do {
          n /= i;
        } <span class="hljs-keyword">while</span> ((n % i) == <span class="hljs-number">0</span>);
      }

      <span class="hljs-comment">// Placing artificial delays in tight loops will also lead to</span>
      <span class="hljs-comment">// sub-optimal results. :-)</span>
      delay(<span class="hljs-number">100</span>);
    }

    <span class="hljs-keyword">if</span> (n != <span class="hljs-number">1</span>) {
      logger.info(<span class="hljs-string">&quot;Found factor &quot;</span> + n);
      factors.addElement(<span class="hljs-keyword">new</span> Integer(n));
    }

    <span class="hljs-keyword">int</span> len = factors.size();

    <span class="hljs-keyword">int</span>[] result = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[len];

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; len; i++) {
      result[i] = ((Integer) factors.elementAt(i)).intValue();
    }

    <span class="hljs-comment">// clean up</span>
    MDC.remove(<span class="hljs-string">&quot;client&quot;</span>);
    MDC.remove(<span class="hljs-string">&quot;number&quot;</span>);

    <span class="hljs-keyword">return</span> result;
  }

  <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">usage</span><span class="hljs-params">(String msg)</span> </span>{
    System.err.println(msg);
    System.err.println(<span class="hljs-string">&quot;Usage: java chapters.mdc.NumberCruncherServer configFile\n&quot;</span> +
      <span class="hljs-string">&quot;   where configFile is a logback configuration file.&quot;</span>);
    System.exit(<span class="hljs-number">1</span>);
  }

  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">delay</span><span class="hljs-params">(<span class="hljs-keyword">int</span> millis)</span> </span>{
    <span class="hljs-keyword">try</span> {
      Thread.sleep(millis);
    } <span class="hljs-keyword">catch</span> (InterruptedException e) {
    }
  }

  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
    <span class="hljs-keyword">if</span> (args.length != <span class="hljs-number">1</span>) {
      usage(<span class="hljs-string">&quot;Wrong number of arguments.&quot;</span>);
    }

    String configFile = args[<span class="hljs-number">0</span>];

    <span class="hljs-keyword">if</span> (configFile.endsWith(<span class="hljs-string">&quot;.xml&quot;</span>)) {
      <span class="hljs-keyword">try</span> {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        JoranConfigurator configurator = <span class="hljs-keyword">new</span> JoranConfigurator();
        configurator.setContext(lc);
        lc.reset();
        configurator.doConfigure(args[<span class="hljs-number">0</span>]);
      } <span class="hljs-keyword">catch</span> (JoranException je) {
        je.printStackTrace();
      }
    }

    NumberCruncherServer ncs;

    <span class="hljs-keyword">try</span> {
      ncs = <span class="hljs-keyword">new</span> NumberCruncherServer();
      logger.info(<span class="hljs-string">&quot;Creating registry.&quot;</span>);

      Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
      registry.rebind(<span class="hljs-string">&quot;Factor&quot;</span>, ncs);
      logger.info(<span class="hljs-string">&quot;NumberCruncherServer bound and ready.&quot;</span>);
    } <span class="hljs-keyword">catch</span> (Exception e) {
      logger.error(<span class="hljs-string">&quot;Could not bind NumberCruncherServer.&quot;</span>, e);

      <span class="hljs-keyword">return</span>;
    }
  }
}
</code></pre>
<p><code>factor(int number)</code> &#x7684;&#x5B9E;&#x73B0;&#x7279;&#x522B;&#x7684;&#x76F8;&#x5173;&#x3002;&#x5B83;&#x5728;&#x542F;&#x52A8;&#x7684;&#x65F6;&#x5019;&#x5C06;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x540D;&#x5B57;&#x901A;&#x8FC7; key <em>client</em> &#x653E;&#x5230; <code>MDC</code> &#x4E2D;&#x3002;&#x5BA2;&#x6237;&#x7AEF;&#x901A;&#x8FC7;&#x8BF7;&#x6C42;&#x5C06;&#x6574;&#x6570;&#x653E;&#x5230; <code>MDC</code> &#x4E2D;&#x7684; <em>number</em> &#x8FD9;&#x4E2A; key &#x4E0B;&#x3002;&#x5728;&#x8BA1;&#x7B97;&#x51FA;&#x6574;&#x6570;&#x7684;&#x4E0D;&#x540C;&#x56E0;&#x5B50;&#x4E4B;&#x540E;&#xFF0C;&#x7136;&#x540E;&#x5C06;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x5728;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x4E4B;&#x524D;&#xFF0C;&#x901A;&#x8FC7;&#x8C03;&#x7528; <code>MDC.remove()</code> &#x6E05;&#x9664; <em>client</em> &#x4E0E; <em>number</em> &#x7684;&#x503C;&#x3002;&#x901A;&#x5E38;&#xFF0C;<code>put()</code> &#x64CD;&#x4F5C;&#x5E94;&#x8BE5;&#x7531; <code>remove()</code> &#x64CD;&#x4F5C;&#x6765;&#x5E73;&#x8861;&#x3002;&#x5426;&#x5219;&#x7684;&#x8BDD;&#xFF0C;<code>MDC</code> &#x5C06;&#x4F1A;&#x4FDD;&#x7559;&#x65E7;&#x503C;&#x3002;&#x6211;&#x4EEC;&#x63A8;&#x8350;&#x65E0;&#x8BBA;&#x4F55;&#x65F6;&#xFF0C;&#x5728; finally &#x4EE3;&#x7801;&#x5757;&#x4E2D;&#x8C03;&#x7528; <code>remove</code> &#x64CD;&#x4F5C;&#xFF0C;&#x7528;&#x6765;&#x786E;&#x4FDD;&#x8BE5;&#x64CD;&#x4F5C;&#x7684;&#x6267;&#x884C;&#xFF0C;&#x800C;&#x4E0D;&#x7528;&#x5173;&#x5FC3;&#x4EE3;&#x7801;&#x7684;&#x5177;&#x4F53;&#x6267;&#x884C;&#x8DEF;&#x5F84;&#x3002;</p>
<p>&#x5728;&#x89E3;&#x91CA;&#x5B8C;&#x7406;&#x8BBA;&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x51C6;&#x5907;&#x8FD0;&#x884C;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;&#x901A;&#x8FC7;&#x4E00;&#x4E0B;&#x547D;&#x4EE4;&#x8FD0;&#x884C;&#x8FD9;&#x4E2A;&#x670D;&#x52A1;&#xFF1A;</p>
<pre><code class="lang-bash">java chapters.mdc.NumberCruncherServer src/main/java/chapters/mdc/mdc1.xml
</code></pre>
<p><em>mdc1.xml</em>  &#x7684;&#x5185;&#x5BB9;&#x5982;&#x4E0B;&#xFF1A;</p>
<blockquote>
<p>Example: <em>mdc1.xml</em> </p>
</blockquote>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">configuration</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">appender</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;CONSOLE&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;ch.qos.logback.core.ConsoleAppender&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">layout</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Pattern</span>&gt;</span>%-4r [%thread] %-5level C:%X{client} N:%X{number} - %msg%n<span class="hljs-tag">&lt;/<span class="hljs-name">Pattern</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">layout</span>&gt;</span>       
  <span class="hljs-tag">&lt;/<span class="hljs-name">appender</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">root</span> <span class="hljs-attr">level</span>=<span class="hljs-string">&quot;debug&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">appender-ref</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">&quot;CONSOLE&quot;</span>/&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">root</span>&gt;</span>  
<span class="hljs-tag">&lt;/<span class="hljs-name">configuration</span>&gt;</span>
</code></pre>
<p>&#x6CE8;&#x610F; <em>%X</em> &#x8F6C;&#x6362;&#x7B26;&#x5728; <code>Pattern</code> &#x9009;&#x9879;&#x4E2D;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#x6267;&#x884C; <code>NumberCruncherClient</code> &#x5E94;&#x7528;&#xFF1A;</p>
<pre><code class="lang-bash">java chapters.mdc.NumberCruncherClient hostname
</code></pre>
<p><em>hostname</em> &#x662F; <code>NumberCruncherServer</code> &#x8FD0;&#x884C;&#x7684;&#x4E3B;&#x673A;&#x540D;&#x3002;</p>
<p>&#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x6267;&#x884C;&#x591A;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x670D;&#x52A1;&#x5668;&#x5BF9; 129 &#x8FDB;&#x884C;&#x56E0;&#x6570;&#x5206;&#x89E3;&#xFF0C;&#x968F;&#x540E;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x670D;&#x52A1;&#x5668;&#x5BF9; 71 &#x8FDB;&#x884C;&#x56E0;&#x6570;&#x5206;&#x89E3;&#x3002;&#x670D;&#x52A1;&#x5668;&#x7684;&#x8F93;&#x51FA;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-number">70984</span> [<span class="hljs-function">RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] INFO  C:orion N:129 - Beginning to factor.
70984 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] DEBUG C:orion N:129 - Trying 2 as a factor.
71093 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] DEBUG C:orion N:129 - Trying 3 as a factor.
71093 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] INFO  C:orion N:129 - Found factor 3
71187 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] DEBUG C:orion N:129 - Trying 4 as a factor.
71297 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] DEBUG C:orion N:129 - Trying 5 as a factor.
71390 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] DEBUG C:orion N:129 - Trying 6 as a factor.
71453 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">5</span>)</span>-192.168.1.6] INFO  C:orion N:71 - Beginning to factor.
71453 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">5</span>)</span>-192.168.1.6] DEBUG C:orion N:71 - Trying 2 as a factor.
71484 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] DEBUG C:orion N:129 - Trying 7 as a factor.
71547 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">5</span>)</span>-192.168.1.6] DEBUG C:orion N:71 - Trying 3 as a factor.
71593 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] DEBUG C:orion N:129 - Trying 8 as a factor.
71656 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">5</span>)</span>-192.168.1.6] DEBUG C:orion N:71 - Trying 4 as a factor.
71687 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] DEBUG C:orion N:129 - Trying 9 as a factor.
71750 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">5</span>)</span>-192.168.1.6] DEBUG C:orion N:71 - Trying 5 as a factor.
71797 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] DEBUG C:orion N:129 - Trying 10 as a factor.
71859 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">5</span>)</span>-192.168.1.6] DEBUG C:orion N:71 - Trying 6 as a factor.
71890 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] DEBUG C:orion N:129 - Trying 11 as a factor.
71953 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">5</span>)</span>-192.168.1.6] DEBUG C:orion N:71 - Trying 7 as a factor.
72000 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">4</span>)</span>-192.168.1.6] INFO  C:orion N:129 - Found factor 43
72062 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">5</span>)</span>-192.168.1.6] DEBUG C:orion N:71 - Trying 8 as a factor.
72156 [RMI TCP <span class="hljs-title">Connection</span><span class="hljs-params">(<span class="hljs-number">5</span>)</span>-192.168.1.6] INFO  C:orion N:71 - Found factor 71
</span></code></pre>
<p>&#x4ECE;&#x4E0A;&#x9762;&#x7684;&#x8F93;&#x51FA;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#x5BA2;&#x6237;&#x7AEF;&#x8FD0;&#x884C;&#x5728;&#x4E00;&#x4E2A;&#x53EB;&#x505A; <em>orion</em> &#x7684;&#x4E3B;&#x673A;&#x4E0A;&#x3002;&#x5C3D;&#x7BA1;&#x670D;&#x52A1;&#x5668;&#x5728;&#x72EC;&#x7ACB;&#x7684;&#x7EBF;&#x7A0B;&#x4E2D;&#x51E0;&#x4E4E;&#x540C;&#x65F6;&#x5904;&#x7406;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x4F46;&#x662F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>MDC</code> &#x6765;&#x533A;&#x5206;&#x6BCF;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x65E5;&#x5FD7;&#x8F93;&#x51FA;&#x3002;&#x793A;&#x4F8B;&#x4E2D;&#x7684;&#x662F;&#x901A;&#x8FC7; <em>number</em> &#x6765;&#x8FDB;&#x884C;&#x6807;&#x8BC6;&#x3002;</p>
<p>&#x7EC6;&#x5FC3;&#x7684;&#x8BFB;&#x8005;&#x53EF;&#x80FD;&#x5DF2;&#x7ECF;&#x6CE8;&#x610F;&#x5230;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7EBF;&#x7A0B;&#x540D;&#x6765;&#x533A;&#x5206;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x3002;&#x5982;&#x679C;&#x670D;&#x52A1;&#x7AEF;&#x91CD;&#x590D;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#xFF0C;&#x90A3;&#x4E48;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x540D;&#x53EF;&#x80FD;&#x4F1A;&#x5BFC;&#x81F4;&#x4E00;&#x4E0B;&#x8BEF;&#x89E3;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x7ED9;&#x5B9A;&#x7EBF;&#x7A0B;&#x5728;&#x670D;&#x52A1;&#x5B8C;&#x8BF7;&#x6C42;&#x4E4B;&#x540E;&#xFF0C;&#x63A5;&#x7740;&#x53BB;&#x670D;&#x52A1;&#x4E0B;&#x4E00;&#x4E2A;&#x8BF7;&#x6C42;&#xFF0C;&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5F88;&#x96BE;&#x53BB;&#x533A;&#x5206;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x3002;&#x56E0;&#x4E3A; <code>MDC</code> &#x662F;&#x53D7;&#x5E94;&#x7528;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x7684;&#x63A7;&#x5236;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x4F1A;&#x53D7;&#x8FD9;&#x4E2A;&#x5F71;&#x54CD;&#x3002;(&#x662F;&#x5417;&#xFF1F;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x5C31;&#x8FD9;&#x4E48;&#x53EF;&#x9760;&#x5417;&#xFF1F;)</p>
<h3 id="&#x81EA;&#x52A8;&#x83B7;&#x53D6;-mdc">&#x81EA;&#x52A8;&#x83B7;&#x53D6; <code>MDC</code></h3>
<p>&#x6B63;&#x5982;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x770B;&#x5230;&#x7684;&#xFF0C;&#x5BF9;&#x591A;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x8FDB;&#x884C;&#x8BF7;&#x6C42;&#x65F6;&#xFF0C;<code>MDC</code>  &#x975E;&#x5E38;&#x6709;&#x7528;&#x3002;&#x5728; web &#x5E94;&#x7528;&#x4E2D;&#x7BA1;&#x7406;&#x7528;&#x6237;&#x8BA4;&#x8BC1;&#xFF0C;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x662F;&#x5728; <code>MDC</code> &#x4E2D;&#x8BBE;&#x7F6E;&#x7528;&#x6237;&#x540D;&#xFF0C;&#x5728;&#x7528;&#x6237;&#x9000;&#x51FA;&#x65F6;&#x8FDB;&#x884C;&#x79FB;&#x9664;&#x3002;&#x4E0D;&#x5E78;&#x7684;&#x662F;&#xFF0C;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x6280;&#x672F;&#x5E76;&#x4E0D;&#x603B;&#x662F;&#x53EF;&#x9760;&#x7684;&#x3002;&#x56E0;&#x4E3A; <code>MDC</code> &#x662F;&#x5728;&#x5355;&#x7EBF;&#x7A0B;&#x7684;&#x57FA;&#x7840;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x91CD;&#x590D;&#x4F7F;&#x7528;&#x7EBF;&#x7A0B;&#x53EF;&#x80FD;&#x4F1A;&#x5BFC;&#x81F4; <code>MDC</code> &#x5305;&#x542B;&#x9519;&#x8BEF;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x5728;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x65F6;&#xFF0C;<code>MDC</code> &#x4E2D;&#x5305;&#x542B;&#x7684;&#x4FE1;&#x606F;&#x4E00;&#x76F4;&#x90FD;&#x662F;&#x6B63;&#x786E;&#x7684;&#x3002;&#x4E00;&#x79CD;&#x53EF;&#x80FD;&#x7684;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x662F;&#x5728;&#x5F00;&#x59CB;&#x5904;&#x7406;&#x4E4B;&#x524D;&#x5B58;&#x50A8;&#x7528;&#x6237;&#x540D;&#xFF0C;&#x7ED3;&#x675F;&#x65F6;&#x8FDB;&#x884C;&#x79FB;&#x9664;&#x3002;<a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/Filter.html" target="_blank"><code>Filter</code></a> &#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5904;&#x7406;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x3002;</p>
<p>&#x5728; servlet &#x8FC7;&#x6EE4;&#x5668; <code>doFilter</code> &#x65B9;&#x6CD5;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; request (&#x6216;&#x8005; cookie) &#x83B7;&#x53D6;&#x7528;&#x6237;&#x7684;&#x76F8;&#x5173;&#x4FE1;&#x606F;&#xFF0C;&#x5E76;&#x5B58;&#x5230; <code>MDC</code> &#x4E2D;&#x3002;&#x5176;&#x5B83;&#x8FC7;&#x6EE4;&#x5668;&#x6216;&#x8005; servlet &#x540E;&#x7EED;&#x7684;&#x5904;&#x7406;&#x4F1A;&#x4ECE; <code>MDC</code> &#x4E2D;&#x53D7;&#x76CA;&#x3002;&#x6700;&#x540E;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x7684; servlet &#x8FC7;&#x6EE4;&#x5668;&#x518D;&#x6B21;&#x83B7;&#x5F97;&#x63A7;&#x5236;&#x65F6;&#xFF0C;&#x5C31;&#x6709;&#x673A;&#x4F1A;&#x53BB;&#x6E05;&#x9664; MDC &#x4E2D;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x8FC7;&#x6EE4;&#x5668;&#x7684;&#x5B9E;&#x73B0;&#xFF1A;</p>
<blockquote>
<p>  Example: <em>UserServletFilter.java</em></p>
</blockquote>
<pre><code class="lang-java"><span class="hljs-keyword">package</span> chapters.mdc;

<span class="hljs-keyword">import</span> java.io.IOException;
<span class="hljs-keyword">import</span> java.security.Principal;

<span class="hljs-keyword">import</span> javax.servlet.Filter;
<span class="hljs-keyword">import</span> javax.servlet.FilterChain;
<span class="hljs-keyword">import</span> javax.servlet.FilterConfig;
<span class="hljs-keyword">import</span> javax.servlet.ServletException;
<span class="hljs-keyword">import</span> javax.servlet.ServletRequest;
<span class="hljs-keyword">import</span> javax.servlet.ServletResponse;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpServletRequest;
<span class="hljs-keyword">import</span> javax.servlet.http.HttpSession;

<span class="hljs-keyword">import</span> org.slf4j.MDC;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserServletFilter</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Filter</span> </span>{

  <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> String USER_KEY = <span class="hljs-string">&quot;username&quot;</span>;

  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">destroy</span><span class="hljs-params">()</span> </span>{
  }

  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doFilter</span><span class="hljs-params">(ServletRequest request, ServletResponse response,
    FilterChain chain)</span> <span class="hljs-keyword">throws</span> IOException, ServletException </span>{

    <span class="hljs-keyword">boolean</span> successfulRegistration = <span class="hljs-keyword">false</span>;

    HttpServletRequest req = (HttpServletRequest) request;    
    Principal principal = req.getUserPrincipal();
    <span class="hljs-comment">// Please note that we could have also used a cookie to </span>
    <span class="hljs-comment">// retrieve the user name</span>

    <span class="hljs-keyword">if</span> (principal != <span class="hljs-keyword">null</span>) {
      String username = principal.getName();
      successfulRegistration = registerUsername(username);
    } 

    <span class="hljs-keyword">try</span> {
      chain.doFilter(request, response);
    } <span class="hljs-keyword">finally</span> {
      <span class="hljs-keyword">if</span> (successfulRegistration) {
        MDC.remove(USER_KEY);
      }
    }
  }

  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">(FilterConfig arg0)</span> <span class="hljs-keyword">throws</span> ServletException </span>{
  }


  <span class="hljs-comment">/**
   * Register the user in the MDC under USER_KEY.
   * 
   * <span class="hljs-doctag">@param</span> username
   * <span class="hljs-doctag">@return</span> true id the user can be successfully registered
   */</span>
  <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">registerUsername</span><span class="hljs-params">(String username)</span> </span>{
    <span class="hljs-keyword">if</span> (username != <span class="hljs-keyword">null</span> &amp;&amp; username.trim().length() &gt; <span class="hljs-number">0</span>) {
      MDC.put(USER_KEY, username);
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
  }
}
</code></pre>
<p>&#x5F53;&#x8FC7;&#x6EE4;&#x5668;&#x7684; <code>doFilter</code> &#x88AB;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x5B83;&#x9996;&#x5148;&#x4F1A;&#x5728; request &#x4E2D;&#x53BB;&#x5BFB;&#x627E; <code>java.security.Principal</code> &#x5BF9;&#x8C61;&#x3002;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x5305;&#x542B;&#x4E86;&#x5F53;&#x524D;&#x8BA4;&#x8BC1;&#x7528;&#x6237;&#x7684;&#x540D;&#x5B57;&#x3002;&#x5982;&#x679C;&#x627E;&#x5230;&#x4E86;&#x7528;&#x6237;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x4F1A;&#x5C06;&#x5B83;&#x6CE8;&#x518C;&#x5230; <code>MDC</code> &#x4E2D;&#x3002;</p>
<p>&#x4E00;&#x4E2A;&#x8FC7;&#x6EE4;&#x5668;&#x94FE;&#x6267;&#x884C;&#x5B8C;&#x6210;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x8FC7;&#x6EE4;&#x5668;&#x4F1A;&#x4ECE; <code>MDC</code> &#x4E2D;&#x79FB;&#x9664;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x521A;&#x624D;&#x6211;&#x4EEC;&#x63CF;&#x8FF0;&#x7684;&#x65B9;&#x6CD5;&#x53EA;&#x662F;&#x5728;&#x8BF7;&#x6C42;&#x671F;&#x95F4;&#xFF0C;&#x6216;&#x8005;&#x7EBF;&#x7A0B;&#x5904;&#x7406;&#x4E2D;&#x624D;&#x53BB;&#x8BBE;&#x7F6E; MDC &#x7684;&#x503C;&#x3002;&#x5176;&#x5B83;&#x7684;&#x7EBF;&#x7A0B;&#x4E0D;&#x4F1A;&#x53D7;&#x5230;&#x5F71;&#x54CD;&#x3002;&#x800C;&#x4E14;&#xFF0C;&#x4EFB;&#x4F55;&#x7ED9;&#x5B9A;&#x7684;&#x7EBF;&#x7A0B;&#x5728;&#x4EFB;&#x4F55;&#x65F6;&#x5019;&#x90FD;&#x4F1A;&#x5305;&#x542B;&#x6B63;&#x786E;&#x7684; MDC &#x6570;&#x636E;&#x3002;</p>
<h3 id="mdc-&#x4E0E;&#x7EBF;&#x7A0B;&#x7BA1;&#x7406;">MDC &#x4E0E;&#x7EBF;&#x7A0B;&#x7BA1;&#x7406;</h3>
<p>MDC &#x7684;&#x526F;&#x672C;&#x4E0D;&#x80FD;&#x603B;&#x662F;&#x7531;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x4ECE;&#x521D;&#x59CB;&#x5316;&#x7EBF;&#x7A0B;&#x7EE7;&#x627F;&#x3002;&#x5F53;&#x4F7F;&#x7528; <code>java.util.concurrent.Executors</code> &#x8FDB;&#x884C;&#x7EBF;&#x7A0B;&#x7BA1;&#x7406;&#x65F6;&#x5C31;&#x662F;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x3002;&#x4F8B;&#x5982;&#xFF0C;<code>newCachedThreadPool</code> &#x65B9;&#x6CD5;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <code>ThreadPoolExecutor</code>&#xFF0C;&#x8DDF;&#x5176;&#x5B83;&#x7EBF;&#x7A0B;&#x6C60;&#x4EE3;&#x7801;&#x4E00;&#x6837;&#xFF0C;&#x6709;&#x7740;&#x590D;&#x6742;&#x7684;&#x7EBF;&#x7A0B;&#x521B;&#x5EFA;&#x903B;&#x8F91;&#x3002;</p>
<p>&#x5728;&#x8FD9;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x63A8;&#x8350;&#x5728;&#x63D0;&#x4EA4;&#x4EFB;&#x52A1;&#x53BB;&#x6267;&#x884C;&#x4E4B;&#x524D;&#xFF0C;&#x5728;&#x6E90;&#x7EBF;&#x7A0B;&#x4E0A;&#x8C03;&#x7528; <code>MDC.getCopyOfContextMap()</code>&#x3002;&#x5F53;&#x4EFB;&#x52A1;&#x8FD0;&#x884C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5B83;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x52A8;&#x4F5C;&#xFF0C;&#x5E94;&#x8BE5;&#x662F;&#x8C03;&#x7528; <code>MDC.setContextMapValues()</code> &#x5C06;&#x539F;&#x59CB; MDC &#x503C;&#x7684;&#x5B58;&#x50A8;&#x526F;&#x672C;&#x4E0E;&#x65B0;&#x7684; <code>Executor</code> &#x7BA1;&#x7406;&#x7EBF;&#x7A0B;&#x5173;&#x8054;&#x8D77;&#x6765;&#x3002;</p>
<h3 id="mdcinsertingservletfilter">MDCInsertingServletFilter</h3>
<p>&#x5728; web &#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x83B7;&#x53D6;&#x7ED9;&#x5B9A; HTTP &#x8BF7;&#x6C42;&#x7684;&#x4E3B;&#x673A;&#x540D;&#xFF0C;&#x8BF7;&#x6C42; URI &#x4EE5;&#x53CA; user-agent &#x662F;&#x5341;&#x5206;&#x6709;&#x7528;&#x7684;&#x3002;<a href="https://logback.qos.ch/xref/ch/qos/logback/classic/helpers/MDCInsertingServletFilter.html" target="_blank"><code>MDCInsertingServletFilter</code></a> &#x901A;&#x8FC7;&#x5982;&#x4E0B;&#x7684; key&#xFF0C;&#x5C06;&#x6570;&#x636E;&#x63D2;&#x5165;&#x5230; MDC &#x4E2D;&#x3002;</p>
<table>
<thead>
<tr>
<th>MDC key</th>
<th>MDC value</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>req.remoteHost</code></td>
<td>&#x7531; <a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getRemoteHost%28%29" target="_blank">getRemoteHost()</a> &#x8FD4;&#x56DE;</td>
</tr>
<tr>
<td><code>req.xForwardedFor</code></td>
<td>&#x8BF7;&#x6C42;&#x5934; <a href="http://en.wikipedia.org/wiki/X-Forwarded-For" target="_blank">&quot;X-Forwarded-For&quot;</a> &#x7684;&#x503C;</td>
</tr>
<tr>
<td><code>req.method</code></td>
<td>&#x7531; <a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html#getMethod%28%29" target="_blank">getMethod()</a> &#x8FD4;&#x56DE;</td>
</tr>
<tr>
<td><code>req.requestURI</code></td>
<td>&#x7531; <a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html#getRequestURI%28%29" target="_blank">getRequestURI()</a> &#x8FD4;&#x56DE;</td>
</tr>
<tr>
<td><code>req.requestURL</code></td>
<td>&#x7531; <a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html#getRequestURL%28%29" target="_blank">getRequestURL()</a> &#x8FD4;&#x56DE;</td>
</tr>
<tr>
<td><code>req.queryString</code></td>
<td>&#x7531; <a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html#getQueryString%28%29" target="_blank">getQueryString()</a> &#x8FD4;&#x56DE;</td>
</tr>
<tr>
<td><code>req.userAgent</code></td>
<td>&#x8BF7;&#x6C42;&#x5934; &quot;User-Agent&quot; &#x7684;&#x503C;</td>
</tr>
</tbody>
</table>
<p>&#x60F3;&#x8981;&#x4F7F;&#x7528; <code>MDCInsertingServletFilter</code>&#xFF0C;&#x9700;&#x8981;&#x5728; <em>web.xml</em> &#x4E2D;&#x6DFB;&#x52A0;&#x4EE5;&#x4E0B;&#x884C;&#xFF1A;</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">filter</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">filter-name</span>&gt;</span>MDCInsertingServletFilter<span class="hljs-tag">&lt;/<span class="hljs-name">filter-name</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">filter-class</span>&gt;</span>
    ch.qos.logback.classic.helpers.MDCInsertingServletFilter
  <span class="hljs-tag">&lt;/<span class="hljs-name">filter-class</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">filter</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">filter-mapping</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">filter-name</span>&gt;</span>MDCInsertingServletFilter<span class="hljs-tag">&lt;/<span class="hljs-name">filter-name</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">url-pattern</span>&gt;</span>/*<span class="hljs-tag">&lt;/<span class="hljs-name">url-pattern</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">filter-mapping</span>&gt;</span>
</code></pre>
<p><strong>&#x5982;&#x679C;&#x4F60;&#x7684; web &#x5E94;&#x7528;&#x6709;&#x591A;&#x4E2A;&#x8FC7;&#x6EE4;&#x5668;&#xFF0C;&#x8BF7;&#x786E;&#x4FDD; <code>MDCInsertingServletFilter</code> &#x5728;&#x5176;&#x5B83;&#x8FC7;&#x6EE4;&#x5668;&#x4E4B;&#x524D;&#x58F0;&#x660E; </strong>&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5047;&#x8BBE; web &#x5E94;&#x7528;&#x7684;&#x4E3B;&#x8981;&#x5904;&#x7406;&#x662F;&#x5728;&#x8FC7;&#x6EE4;&#x5668; &apos;F&apos; &#x4E2D;&#x5B8C;&#x6210;&#xFF0C;&#x90A3;&#x4E48;&#x5982;&#x679C; <code>MDCInsertingServletFilter</code> &#x5728; &apos;F&apos; &#x4E4B;&#x540E;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x90A3;&#x4E48;&#x5728; &apos;F&apos; &#x4E2D;&#x88AB;&#x8C03;&#x7528;&#x7684;&#x4EE3;&#x7801;&#x5C06;&#x770B;&#x4E0D;&#x5230; <code>MDCInsertingServletFilter</code> &#x8BBE;&#x7F6E;&#x7684; MDC &#x7684;&#x503C;&#x3002;(&#x8FD9;&#x4E0D;&#x662F;&#x5E9F;&#x8BDD;&#x5417;......)</p>
<p>&#x4E00;&#x65E6;&#x4F7F;&#x7528;&#x4E86;&#x8BE5;&#x8FC7;&#x6EE4;&#x5668;&#xFF0C;&#x901A;&#x8FC7; %X &#x53EF;&#x4EE5;&#x8F93;&#x51FA;&#x76F8;&#x5BF9;&#x5E94;&#x7684; MDC &#x7684;&#x503C;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x60F3;&#x8981;&#x5728;&#x4E00;&#x884C;&#x4E0A;&#x6253;&#x5370;&#x8FDC;&#x7A0B;&#x4E3B;&#x673A;&#xFF0C;&#x540E;&#x9762;&#x8DDF;&#x7740;&#x8BF7;&#x6C42;&#x7684; URI&#xFF0C;&#x4EE4;&#x4E00;&#x884C;&#x6253;&#x5370;&#x65E5;&#x5FD7;&#xFF0C;&#x540E;&#x9762;&#x8DDF;&#x7740;&#x65E5;&#x5FD7;&#x6D88;&#x606F;&#x3002;&#x90A3;&#x4E48;&#x4F60;&#x5E94;&#x8BE5;&#x5982;&#x4E0B;&#x8BBE;&#x7F6E; <code>PatternLayout</code>&#xFF1A;</p>
<pre><code class="lang-xml">%X{req.remoteHost} %X{req.requestURI}%n%d - %m%n
</code></pre>



                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="07第七章Filters.html" class="navigation navigation-prev " aria-label="Previous page: 第七章：Filters">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="09第九章日志隔离.html" class="navigation navigation-next " aria-label="Next page: 第九章：日志隔离">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第八章：MDC","level":"1.9","depth":1,"next":{"title":"第九章：日志隔离","level":"1.10","depth":1,"path":"09第九章日志隔离.md","ref":"09第九章日志隔离.md","articles":[]},"previous":{"title":"第七章：Filters","level":"1.8","depth":1,"path":"07第七章Filters.md","ref":"07第七章Filters.md","articles":[]},"dir":"ltr"},"config":{"plugins":["add-js-css","myseo"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"add-js-css":{"js":["./lib/site.js"]},"myseo":{"description":"logback中文网包含了logback中文文档，最新资讯，应用案例等。logback 继承自 log4j，它建立在有十年工业经验的日志系统之上。它比其它所有的日志系统更快并且更小，包含了许多独特并且有用的特性。","keywords":"logback中文文档, logback api文档, logback log4j,springboot logback,logback日志类型","titlePrefix":"logback中文网|"},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","keywords":"","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"logback中文手册","gitbook":"*","description":""},"file":{"path":"08第八章MDC.md","mtime":"2019-02-12T05:09:53.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-02-24T03:19:05.191Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-add-js-css/fda70cfc36440304bb976a11f47eb151-site.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

